package cn.xiu.drugstore.service.impl;

import java.util.List;

import cn.xiu.drugstore.dao.StaffDao;
import cn.xiu.drugstore.dao.impl.StaffDaoImpl;
import cn.xiu.drugstore.pojo.PageBean;
import cn.xiu.drugstore.pojo.Staff;
import cn.xiu.drugstore.service.StaffService;
/**
 * 员工业务功能实现类
 * @author 陈亚鹏
 *
 */
public class StaffServiceImpl implements StaffService {
	StaffDao dao=new StaffDaoImpl();
	/**
	 * 返回全部员工对象集合
	 */
	@Override
	public List<Staff> findStaff() {
		try {
			return dao.queryAllStaff();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 显示指定id的员工信息
	 */
	@Override
	public Staff staffInfo(int id) {
		try {
			return dao.queryStaffById(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 添加一位员工信息
	 */
	@Override
	public boolean registerStaff(Staff staff) {
		StaffDao nd = new StaffDaoImpl();
		int result = -1;
		try {
			result = nd.addStaff(staff);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (result > 0) {
			return true;
		} else {
			return false;
		}
	}
	/**
	 * 删除
	 */
	@Override
	public boolean deleteStaff(int id) {
		StaffDao nd = new StaffDaoImpl();
		int result = -1;
		try {
			result = nd.deleteStaff(id);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (result > 0) {
			return true;
		} else {
			return false;
		}
	}
	/**
	 * 根据姓名查找员工
	 */
	@Override
	public Staff findByName(String name) {
		try {
			return dao.queryStaffByName(name);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
/**
 * 查询总记录数
 */
	@Override
	public int queryTotalRecord(String queryName, int roleId) {
		try {
			return dao.queryTotalRecord(queryName, roleId);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}
	/**
	 * 查询总页数
	 */
	@Override
	public int queryTotalPage(int pageSize, String queryName, int roleId) {
		PageBean<Staff> pageBean=new PageBean<Staff>();
		//总记录数
		int totalCount=this.queryTotalRecord(queryName, roleId);
		//设置每页显示的记录数
		pageBean.setPageSize(pageSize);
		//计算总页数
		pageBean.setTotalCount(totalCount);
		return pageBean.getTotalPageCount();
	}
	/**
	 * 根据页数显示（查询）员工
	 */
	@Override
	public PageBean<Staff> queryStaffByPageNo(int pageNo, int pageSize,
			String queryName, int roleId) {
		PageBean<Staff> pageBean = new PageBean<Staff>();
		pageBean.setCurrPageNo(pageNo);
		pageBean.setPageSize(pageSize);
		pageBean.setTotalCount(this.queryTotalRecord(queryName, roleId));

		// 偏移量
		int offset = (pageNo - 1) * pageSize;
		List<Staff> list;
		try {
			list = dao.queryStaffByOffset(offset, pageSize, queryName, roleId);
			pageBean.setList(list);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return pageBean;
	}

}
